#!/bin/bash
#chkconfig: 2345 70 30  #234
VIP={{lvs_vip}}
OLD_VIP=$2
ip_dev=lo
source /etc/bashrc
source /etc/rc.d/init.d/functions
RETVAL=0


rs_status(){
    # status == 0 is right
    ip add sh ${ip_dev} |grep ${VIP} &> /dev/null
    vip_status=$?
    # rouer status
    vip_route=$(ip rou sh root ${VIP} scope link)
    [ -n "${vip_route}" ] && rou_status=0 || rou_status=1
    # kernel argument value
    lo_arp_ignore_val=$(cat /proc/sys/net/ipv4/conf/lo/arp_ignore)
    #[ ${lo_arp_ignore_val} == 0 ] && lo_arp_ignore_status=1 || lo_arp_ignore_status=0
    lo_arp_announce_val=$(cat /proc/sys/net/ipv4/conf/lo/arp_announce)
    all_arp_ignore_val=$(cat /proc/sys/net/ipv4/conf/all/arp_ignore)
    all_arp_announce_val=$(cat /proc/sys/net/ipv4/conf/all/arp_announce)
    for item in lo_arp_ignore_val lo_arp_announce_val all_arp_ignore_val all_arp_announce_val;do
        item_status=${item/val/status}
        [ ${!item} == 0 ] && eval ${item_status}=1 || eval ${item_status}=0
    done
    for stat in vip_status rou_status lo_arp_ignore_status lo_arp_announce_status all_arp_ignore_status all_arp_announce_status;do
        stat_val=${!stat}
        if [ ${stat_val} -ne 0 ];then
            echo "RealServer is abnormal" >&2
            return ${stat_val}
        fi
    done 
    echo "RealServer is ok"
    return ${vip_status}
}
rh_status() {
    rs_status
    RETVAL=$?
    return $RETVAL
}

rs_start(){
    #ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
    ip add add ${VIP}/32 broadcast ${VIP} dev ${ip_dev} label ${ip_dev}:0
    #ip add add ${VIP}/32 dev lo label lo:0
    #/sbin/route add -host $VIP dev lo:0
    ip rou add ${VIP}/32 via ${VIP} dev ${ip_dev} scope link
    echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
    echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
    echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
    echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
    echo "RealServer Start OK"
}

rs_stop(){
    #ifconfig lo:0 down
    ip add del ${VIP}/32 dev ${ip_dev}
    ip rou del ${VIP}/32 dev ${ip_dev}
    #route del $VIP >/dev/null 2>&1
    echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
    echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
    echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
    echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
    echo "RealServer Stoped"
}

rs_delete_ip(){
    default_stat=13
    if [ -n "${OLD_VIP}" ];then
        ip_del_stat=${default_stat}
        rou_del_stat=${default_stat}
        if ip a sh ${ip_dev} |grep "${OLD_VIP}";then
            echo "ip add del ${VIP} dev ${ip_dev}"
            ip add del ${VIP} dev ${ip_dev}
            ip_del_stat=$?
        fi
        
        if ip rou sh |gerp "${OLD_VIP}";then
            echo "ip rou del ${VIP} dev ${ip_dev}"
            ip rou del ${VIP} dev ${ip_dev}
            rou_del_stat=$?
        fi

    else
        echo "${OLD_VIP} is null."
        return 0
    fi 
    
    if [ ${ip_del_stat} == ${default_stat} ] && [ ${rou_del_stat} == ${default_stat} ];then
        return 0
        RETVAL=0
    else
        if [ ${ip_del_stat} == 0 ] && [ ${rou_del_stat} == 0 ];then
            return 10
            RETVAL=10
        else
            echo "ip_del_stat ${ip_del_stat}, rou_del_stat: ${rou_del_stat}"
            return 11
            RETVAL=11
        fi
    fi
    
}

case "$1" in
start)
    rs_start
    ;;
stop)
    rs_stop
    ;;
status)
    rh_status 
    ;;
restart)
    rs_stop
    rs_start
    ;;
delete)
    rs_delete_ip    
    ;;
*)
    echo "Usage: $0 {start|stop|status|restart}"
    exit 1
esac
exit ${RETVAL}
